package cn.com.gridinfo.sooc.core.expand.callname.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.widget.ImageView;

import com.jeremy.arad.utils.Log;

import java.lang.reflect.Field;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.expand.callname.adapter.SignInfoAdapter;
import cn.com.gridinfo.sooc.core.expand.callname.fragment.QueXiFragment;
import cn.com.gridinfo.sooc.core.expand.callname.fragment.ShiDaoFragment;
import cn.com.gridinfo.sooc.core.expand.callname.fragment.YingDaoFragment;
import cn.com.gridinfo.sooc.utils.IntentUtil;
import cn.com.gridinfo.sooc.utils.widget.MySpeedScroller;

/**
 * 签到详情界面
 *
 * @author yingchunli
 */
public class SignInfoActivity extends BaseSoocActivity {


    @InjectView(R.id.sliding_tabs_signinfo)
    TabLayout tabLayout;//标签

    @InjectView(R.id.viewpager_signinfo)
    ViewPager mViewPager;//ViewPager
    String ktid;//课堂id
    String kcid;//课程id
    String pzqd;//拍照签到

    String[] tabText = {"实到", "应到", "缺席"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expand_signin_info);
        ButterKnife.inject(this);
        ktid = getIntent().getStringExtra("ktid");
        kcid = getIntent().getStringExtra("kcid");
        pzqd = getIntent().getStringExtra("pzqd");
        Log.d("签到详情 ktid->" + ktid);
        Log.d("签到详情 kcid->" + kcid);
        Log.d("签到详情 pzqd->" + pzqd);
        initView();
    }

    private void initView() {
        initViewPagerScroll();
        SignInfoAdapter adapter = new SignInfoAdapter(getSupportFragmentManager());
        adapter.addFragment(ShiDaoFragment.newInstance(ktid + "," + kcid + "," + pzqd), "实到");
        adapter.addFragment(YingDaoFragment.newInstance(ktid), "应到");
        adapter.addFragment(QueXiFragment.newInstance(ktid + "," + kcid), "缺席");

        tabLayout.addTab(tabLayout.newTab().setText(tabText[0]));
        tabLayout.addTab(tabLayout.newTab().setText(tabText[1]));
        tabLayout.addTab(tabLayout.newTab().setText(tabText[2]));
        mViewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(mViewPager);
        mViewPager.setOffscreenPageLimit(2);
    }

    /**
     * 设置ViewPager的滑动速度
     */
    private void initViewPagerScroll() {
        try {
            Field mScroller = null;
            mScroller = ViewPager.class.getDeclaredField("mScroller");
            mScroller.setAccessible(true);
            MySpeedScroller scroller = new MySpeedScroller(mViewPager.getContext());
            mScroller.set(mViewPager, scroller);
        } catch (NoSuchFieldException e) {

        } catch (IllegalArgumentException e) {

        } catch (IllegalAccessException e) {

        }
    }

    @Override
    public String setupToolBarTitle() {
        return "签到详情";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarLeftClick() {
        exitActivity();
    }

    /**
     * 关闭本界面的操作
     */
    void exitActivity() {
        int change = Integer.parseInt(ShiDaoFragment.countRen.getText().toString().trim());
        Log.d("change  SignInfo=" + change);
        Intent intent = new Intent();
        intent.putExtra("change", change);
        setResult(RESULT_OK, intent);
        finish();
    }

    /**
     * 按下底部返回按钮
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            exitActivity();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
